﻿package cn.poco.pepsi.activity.init;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import cn.poco.pepsi.R;
import cn.poco.pepsi.TabActivity;
import cn.poco.pepsi.bean.RemindSum;
import cn.poco.pepsi.service.impl.RemindImpl;
import cn.poco.pepsi.util.Cons;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Window;

public class InitActivity extends Activity {

	private String cityName, cityCode;
	private RemindSum reSum;

	

	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == 1) {
				//非第一次启动，
				System.out.println("非初始化程序------------>启动TabActivity");
				SharedPreferences preferences = getSharedPreferences(Cons.GPS_CITY_CODE, Context.MODE_PRIVATE);
				Intent tabInent = new Intent(InitActivity.this, TabActivity.class);
				if (preferences.contains(Cons.CITY_CODE)) {
					cityCode = preferences.getString(Cons.CITY_CODE, "123123");
					cityName = preferences.getString(Cons.CITY_NAME, "");
					
					Bundle bundle = new Bundle();
					bundle.putString(Cons.CITY_NAME, cityName);
					bundle.putString(Cons.CITY_CODE, cityCode);
					bundle.putSerializable(Cons.INIT_REMINDSUM, reSum);
					tabInent.putExtras(bundle);
					startActivity(tabInent);
				} else {
					
					System.out.println("初始化程序------------>启动SwitchCityActivity");
					Intent intent = new Intent(InitActivity.this, SwitchCityActivity.class);
					startActivity(intent);
				}
			}
			finish();
			handler.removeMessages(1);
		}
	};

	void getRemindSum() {
		SharedPreferences sf = InitActivity.this.getSharedPreferences(Cons.SFPocoUser, 0);
		final String pocoid = sf.getString(Cons.SFPocoId, "0");
		new Thread(new Runnable() {
			@Override
			public void run() {
				RemindImpl ri = new RemindImpl();
				try {
					if (!pocoid.equals("0")) {
						reSum = ri.getRemaindSum(pocoid);
					} else {
						reSum = null;
					}
					handler.sendEmptyMessageDelayed(1,3000);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.init);
		
		// 判断网络类型
		ConnectivityManager connectivityManager = (ConnectivityManager) InitActivity.this
				.getSystemService(Context.CONNECTIVITY_SERVICE);// 获取系统的连接服务
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();// 获取网络的连接情况
		if (activeNetInfo != null && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
			Cons.WIFI_CONNECT = true;
		}

		try {
			InputStream inputStream = getAssets().open("city.db");
			File file = getDatabasePath("city.db");
			File fileDir = file.getParentFile();
			if (!fileDir.exists()) {
				fileDir.mkdir();
			}
			FileOutputStream fileOutputStream = new FileOutputStream(file);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = inputStream.read(buffer)) != -1) {
				fileOutputStream.write(buffer, 0, len);
			}
			inputStream.close();
			fileOutputStream.close();
		} catch (Exception e) {

		}
		getRemindSum();
	}
	
	
}
